/*
 * Copyright (c) 2010-2025. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.eventhandling.sequencing;

import jakarta.annotation.Nonnull;
import org.axonframework.eventhandling.EventMessage;
import org.axonframework.messaging.unitofwork.ProcessingContext;
import org.axonframework.messaging.unitofwork.ProcessingLifecycle;

import java.util.Optional;
import java.util.function.Function;

/**
 * Interface to a policy definition for concurrent processing, for example event handling.
 * <p/>
 * Some implementations are provided by default: <ul>
 * <li>{@link SequentialPolicy}: Requires that all
 * tasks are handled in the order they arrive at the processor. This also means that at most 1 thread is processing
 * tasks for this processor at any time.</li>
 * <li>{@link FullConcurrencyPolicy}: Allows each tasks to be handled independently of any other tasks. Tasks
 * processing will typically start in the same order the tasks were scheduled in, although no guarantees can be given
 * about the actual processing order.</li>
 * <li>{@link SequentialPerAggregatePolicy}: Default policy. Will force events (only supports Event Handling tasks)
 * generated by the same aggregate to be handled sequentially. At most one thread will be processing events of a single
 * aggregate at any time</li>
 * </ul>
 *
 * @param <T> The type of object representing the processing instruction for the event.
 * @author Allard Buijze
 * @since 0.3
 */
@FunctionalInterface
public interface SequencingPolicy {

    /**
     * Returns the sequence identifier for the given {@code event}. When two events have the same identifier (as defined
     * by their equals method), they will be executed sequentially. A {@code Optional#empty()} value indicates that
     * there are no sequencing requirements for the handling of this event.
     * <p>
     * The {@code Optional#empty()} should ONLY be returned when the policy cannot determine a sequence identifier for
     * the given event. This typically happens when the policy is not applicable for the specific event type. When
     * {@code Optional#empty()} is returned, it is up to the component using this policy to provide a default behavior,
     * use another policy, or throw an exception / react in any other way - as appropriate.
     *
     * @param event   The event for which to get the sequencing identifier.
     * @param context The processing context in which the event is being handled. This instance of the
     *                {@link ProcessingContext} doesn't allow you to register phases actions by for example
     *                {@link ProcessingContext#on} or retrieving components by {@link ProcessingContext#component}.
     * @return A sequence identifier for the given event, or {@code Optional#empty()} if this policy cannot determine a
     * sequence identifier for the given event.
     */
    Optional<Object> getSequenceIdentifierFor(@Nonnull EventMessage event, @Nonnull ProcessingContext context);
}
